package com.j2eefast.compressor.maven;

import com.google.javascript.jscomp.CommandLineRunner;
import cn.hutool.core.util.ReflectUtil;
import org.apache.maven.plugin.logging.Log;
import java.io.IOException;
import java.util.List;

/**
 * GoogleCompiler
 * @author ThinkGem
 * @author J2eeFAST
 * @version 2020-3-9
 */
public class GoogleCompiler extends CommandLineRunner {

    private static GoogleCompiler compiler;

    protected GoogleCompiler(List<String> args) {
        super(args.toArray(new String[args.size()]));
    }

    public void run(Log log) {
        if (this.shouldRunCompiler()) {
            try {
                this.doRun();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }


    public static void run(List<String> a, Log log) {
        log.debug("开始 Google Closure Compiler: " + a.toString());
        //new GoogleCompiler(a).run(log);
		if (compiler == null) {
			compiler = new GoogleCompiler(a);
		}else {
			compiler.update(a);
		}
		compiler.run(log);
    }

    private void update(List<String> a) {
        String[] args = a.toArray(new String[a.size()]);
        //直接执行
        ReflectUtil.invoke(this,"initConfigFromFlags", args,System.out, System.err);
    }
//    public static void main(String[] args) {
          //指令帮助
//        CommandLineRunner.main(new String[]{"--help"});
//        CommandLineRunner.main(new String[] {"--version"});
//        System.out.println("---->>>>");
//    }

}
